﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using TTGame;
using TTGameEngine;

/*
 * DamageReceiverLineCollider
 *
 *
 *
 * 依赖：
 *
 * ！不写重复代码！
 * @author TTun
 * @date 10/11/2017 5:35:56 PM
 */


public class DamageReceiverLineCollider : DamageReceiverCollider {
    #region Fields

    public Transform bindTF;

    public float speed;
    public float lineLenFactor;

    [SerializeField, ReadOnly]
    float t_rayLen = 0;
    Vector3 draw_startPos;
    Vector3 draw_direction;
    #endregion Fields

    #region Methods

    public void Init(Transform aBindTF, float aSpeed, float aLineLenFactor, LayerMask layerMask) {
        speed = aSpeed;
        lineLenFactor = aLineLenFactor;

        if(bindTF != null) {
            bindTF = aBindTF;
        } else {
            bindTF = transform;
        }
        layersToHit = layerMask;
        //初始值
        t_rayLen = speed * 0.03f * lineLenFactor;
        init();
    }

    public override float ColliderRage { get { return t_rayLen; } }

    public override void CheckInsideColliderRay() {
        RaycastHit hitInfo;
        t_rayLen = speed * Time.deltaTime * lineLenFactor;
#if UNITY_EDITOR
        draw_startPos = transform.position;
        draw_direction = transform.forward * t_rayLen;
#endif
        if(Physics.Raycast(bindTF.position, bindTF.forward, out hitInfo, t_rayLen, layersToHit.value)) {
            AddCollier(hitInfo.collider);
        }
    }

    public override bool PointInsideCollider(Vector3 aPoint) {
        return false;
    }

    //从人物中心到子弹出生点（枪口）+ 一帧飞行距离。
    //起始点近似取碰撞体当前起点的反向延长线。
    public override void BeginningColliderRay(Vector3 extraCheckPos) {
        RaycastHit hitInfo;
        float extraLen = (extraCheckPos - bindTF.position).magnitude;
        Vector3 startPos = bindTF.position - bindTF.forward * extraLen;
        float rayLen = extraLen + t_rayLen;
#if UNITY_EDITOR
        draw_startPos = startPos;
        draw_direction = bindTF.forward * rayLen;
#endif
        if(Physics.Raycast(startPos, bindTF.forward, out hitInfo, rayLen, layersToHit.value)) {
            AddCollier(hitInfo.collider);
        }
    }

    void OnDrawGizmos() {
        if(DamageReceiverCollider.IsDrawColliderWire == false) {
            return;
        }
        Gizmos.color = Color.green;
        Gizmos.DrawRay(draw_startPos, draw_direction);
    }

    #endregion Methods
}

